function loadJS(url,callback){var script=document.createElement('script'),fn=callback||function(){};script.type='text/javascript';if(script.readyState){script.onreadystatechange=function(){if(script.readyState=='loaded'||script.readyState=='complete'){script.onreadystatechange=null;fn()}}}else{script.onload=function(){fn()}}script.src=url;document.getElementsByTagName('head')[0].appendChild(script)}

var fnSendData2Qt,
	receiverData,
	ReceiveMapData;
	
loadJS('qrc:///weblib/qwebchannel.js', function(){
	// 调用 Qt 对象的方法并传递数据
	new QWebChannel2(qt.webChannelTransport, function (channel) {
		receiverData = channel.objects.receiverData;
	});
	fnSendData2Qt = function(dataString){
		if(receiverData)
			receiverData.fnReceiveData(dataString);
		else console.error("receiverData is not initialized.");
	}
});

var myVisualMap = {
    // min: 0,
    // max: 200,
    text: ['High', 'Low'],
    type: 'continuous',
    realtime: false,
    calculable: false,
    itemWidth:10,
    //itemHeight: '180',
    top: 8,
    //bottom: "bottom",
    inRange: {
        color: ['rgba(34,173,255,.7)', 'yellow', 'orangered']
    },
    textStyle: {
        color: '#FFF',
        fontSize: 12
    },
};

var ChinaMapData = [
    {name: '北京',value: 0 },{name: '天津',value: 0 },  
    {name: '上海',value: 0 },{name: '重庆',value: 0 },  
    {name: '河北',value: 0 },{name: '河南',value: 0 },  
    {name: '云南',value: 0 },{name: '辽宁',value: 0 },  
    {name: '黑龙江',value: 0 },{name: '湖南',value: 0 },  
    {name: '安徽',value: 0 },{name: '山东',value: 0 },  
    {name: '新疆',value: 0 },{name: '江苏',value: 0 },  
    {name: '浙江',value: 0 },{name: '江西',value: 0 },  
    {name: '湖北',value: 0 },{name: '广西',value: 0 },  
    {name: '甘肃',value: 0 },{name: '山西',value: 0 },  
    {name: '内蒙古',value: 0 },{name: '陕西',value: 0 },  
    {name: '吉林',value: 0 },{name: '福建',value: 0 },  
    {name: '贵州',value: 0 },{name: '广东',value: 0 },  
    {name: '青海',value: 0 },{name: '西藏',value: 0 },  
    {name: '四川',value: 0 },{name: '宁夏',value: 0 },  
    {name: '海南',value: 0 },{name: '台湾',value: 0 },  
    {name: '香港',value: 0 },{name: '澳门',value: 0 },
    {name: '南海诸岛',value: 0 }  
];

var mapOption = {
    tooltip: {
        show:true,
        trigger: 'item',
        formatter: '{b}<br/>{c} 台',
        extraCssText: "background-color:rgba(0,0,0,.7); border:none; color:#FFF;"
    },
    visualMap: myVisualMap,
    series: [
        {
            name: '中国',
            type: 'map',
            mapType: 'china',
            //selectedMode: 'multiple',
            //width: '86%',
			layoutCenter: ['53%', '56%'],
			layoutSize: "140%",
            height: '100%',
            left: '0',
            label: {
                show: false,
                textStyle: {
                    color: '#FFF',
                    fontSize: 13
                }
            },
            itemStyle: {
                normal: {
                    borderWidth: 0.3,
                    borderColor:'#082858',
                },
                emphasis: {
                    show: true,
                    areaColor: 'rgba(255,255,255,.7)',
                    label: {
                        show: true,
                        textStyle: {
                            color: '#000'
                        }
                    }
                }                     
            },
            roam: true,
            zoom:1,
            selectedMode: false,
            data: ChinaMapData
        }
    ]
};
var mapChart = echarts.init(document.getElementById("myStationMap"), null, {
	renderer: 'canvas'	//默认是 'canvas'，显式声明避免歧义
});
function fnMapChinaChart(){
	let mapData = [],
		provName = '',
		bita = ['黑龙江','内蒙古','南海诸岛'],
		data = ReceiveMapData.data;
    for (let i in data) {
		provName = data[i].provName;
		if(provName){
			provName = data[i].provName.substr(0,2);
            for(let j in bita){
	            if(provName === bita[j].substr(0,2)){
					provName = bita[j];
					break;
				}
			}
			mapData.push({
				name: provName,
				value: data[i].dCount,
			});
		}
	}
	
	for (let i in ChinaMapData) {
	    for (let j in mapData) {
	        if(mapData[j].name == ChinaMapData[i].name){
	            ChinaMapData[i].value = mapData[j].value;
	            continue;
	        }
	    }
	}
	mapOption.series[0].data = ChinaMapData;	
	mapChart.clear();
	try {
		mapOption && mapChart.setOption(mapOption);
		let message = {'option':'MapLoadFinished'};
		fnSendData2Qt(JSON.stringify(message));
		// 省市点击事件
		mapChart.off('click').on('click',function(data){
			let proJson = pinyin.getFullChars(data.name).toLowerCase();
			if(data.name == '陕西'){
				proJson = proJson+'1';
			}
			if(data.name == '西藏'){
				proJson = 'xizang';
			}
			if(data.name == '重庆'){
				proJson = 'chongqing';
			}
			if(data.name == '内蒙古'){
				proJson = 'neimenggu';
			}
			if(data.name == '台湾' || data.name == '南海诸岛'){
				parent.layer.msg(data.name+'暂无业务');
				return;
			}
			let message = {'option':'currProvinceChanged','proName':data.name, 'proJson':proJson};
			fnSendData2Qt(JSON.stringify(message));
		})
	}catch(err){
		console.log(err);
		return;
	}
}

window.addEventListener("resize", function() {
	if(mapChart != null)
		mapChart.resize();
});
 
var fnJudgement = function(){
	let funOk = typeof fnMapChinaChart === 'function' && fnMapChinaChart instanceof Function ? 1 : 0;
	if (funOk == 1) fnMapChinaChart();
	else{
		setTimeout(function(){
			fnJudgement();
		}, 1000);
	}
}
 /**
  * 接受外部传值
  */
function fnReceiveMapData(data){
	ReceiveMapData = JSON.parse(data);
    if (typeof ReceiveMapData === 'object' && ReceiveMapData !== null){
		if(Array.isArray(ReceiveMapData.data))
			fnJudgement();
		else console.error('Map data error！', Array.isArray(ReceiveMapData.data));
	}
}

/**
 * 通知qt，JS已经加载完成
 */
function fnJavascriptReady(){
	if ((typeof fnSendData2Qt === 'function' && fnSendData2Qt instanceof Function)
		&& (typeof fnReceiveMapData === 'function' && fnReceiveMapData instanceof Function)){
		let message = {'option':'JavascriptReady'};
		fnSendData2Qt(JSON.stringify(message));
	}else{
		setTimeout(function(){
			fnJavascriptReady();
		}, 800);
	}
}
fnJavascriptReady();
